package com.touka.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.touka.common.Result;
import com.touka.entity.AddressBook;
import com.touka.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
  @Autowired
  private AddressBookService addressBookService;

  private static final LambdaUpdateWrapper<AddressBook> updateWrapper = Wrappers.lambdaUpdate(AddressBook.class);
  private static final LambdaQueryWrapper<AddressBook> queryWrapper = Wrappers.lambdaQuery(AddressBook.class);

  /**
   * 新增地址
   *
   * @param addressBook
   * @param session
   * @return
   */
  @PostMapping
  public Result<String> insert(@RequestBody AddressBook addressBook, HttpSession session) {
    log.info("新增地址：{}", addressBook);

    // 获取当前登录用户Id
    Long userId = (Long) session.getAttribute("user");
    addressBook.setUserId(userId);
    addressBook.setCreateUser(userId);

    boolean save = addressBookService.save(addressBook);

    return save ? Result.success("新增成功") : Result.error("新增失败");
  }

  /**
   * 查询当前用户的收货地址
   *
   * @param session
   * @return
   */
  @GetMapping("/list")
  public Result<List<AddressBook>> list(HttpSession session) {
    Long userId = (Long) session.getAttribute("user");
    log.info("当前登录用户Id：{}", userId);
    List<AddressBook> addressBooks = addressBookService.list(queryWrapper.eq(AddressBook::getUserId, userId));
    return Result.success(addressBooks);
  }

  /**
   * 设置默认地址
   *
   * @param addressBook
   * @param session
   * @return
   */
  @PutMapping("/default")
  public Result<String> setDefault(@RequestBody AddressBook addressBook, HttpSession session) {
    Long userId = (Long) session.getAttribute("user");
    log.info("设置默认地址：{}-地址ID：{}", userId, addressBook.getId());
    addressBookService.update(
            updateWrapper
                    .eq(AddressBook::getUserId, userId)
                    .set(AddressBook::getIsDefault, 0)
    );

    addressBookService.update(
            updateWrapper
                    .eq(AddressBook::getUserId, userId)
                    .eq(AddressBook::getId, addressBook.getId())
                    .set(AddressBook::getIsDefault, 1)
    );

    return Result.success("设置成功");
  }

  /**
   * 根据Id获取收货地址信息
   *
   * @param id
   * @return
   */
  @GetMapping("/{id}")
  public Result<AddressBook> getById(@PathVariable Long id) {
    log.info("根据Id获取收货地址信息：{}", id);

    AddressBook addressBook = addressBookService.getById(id);

    return Result.success(addressBook);
  }

  /**
   * 修改地址信息
   *
   * @param addressBook
   * @return
   */
  @PutMapping
  public Result<String> update(@RequestBody AddressBook addressBook) {
    log.info("修改收货地址：{}", addressBook);

    addressBookService.updateById(addressBook);

    return Result.success("修改成功");
  }

  /**
   * 根据id删除收货地址
   *
   * @param ids
   * @return
   */
  @DeleteMapping
  public Result<String> delete(@RequestParam List<Long> ids) {
    log.info("删除收货地址：{}", ids);

    addressBookService.removeByIds(ids);

    return Result.success("删除成功");
  }

  /**
   * 获取默认地址
   *
   * @param session
   * @return
   */
  @GetMapping("/default")
  public Result<AddressBook> getDefault(HttpSession session) {
    log.info("获取默认地址：{}", session.getAttribute("user"));

    AddressBook addressBook = addressBookService.getOne(
            queryWrapper
                    .eq(AddressBook::getUserId, session.getAttribute("user"))
                    .eq(AddressBook::getIsDefault, 1)
    );

    return Result.success(addressBook);
  }
}
